package com.example.fashion.android.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.example.fashion.android.R;


import java.lang.reflect.Field;

/**
 * Created by Administrator on 2016/10/10 0010.
 * 弹出Toast的工具类
 */

public class DialogUtil {

    /**
     * @param context
     * @param title                  标题
     * @param message                内容
     * @param icon                   图标
     * @param confirmOnClickListener 确定执行
     * @param cancelOnClickListener  取消执行
     */
    public static void getDialog(Context context, String title, String message, int icon, OnClickListener confirmOnClickListener, OnClickListener cancelOnClickListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_LIGHT);
        builder.setTitle(title);
        builder.setIcon(icon);
        builder.setMessage(message);
        builder.setPositiveButton("确定", confirmOnClickListener);
        builder.setNegativeButton("取消", cancelOnClickListener);
        builder.show();
    }


    /**
     * @param context
     * @param message                内容
     * @param confirmOnClickListener 确定执行
     * @param cancelOnClickListener  取消执行
     */
    public static void getDialog(Context context,String message, OnClickListener confirmOnClickListener, OnClickListener cancelOnClickListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_LIGHT);
        builder.setTitle("提示");
        builder.setIcon(R.drawable.logo11);
        builder.setMessage(message);
        builder.setPositiveButton("确定", confirmOnClickListener);
        builder.setNegativeButton("取消", cancelOnClickListener);
        builder.show();
    }


    /**
     * 取消不执行Dialog
     * @param context
     * @param message                内容
     * @param confirmOnClickListener 确定执行
     */
    public static void getDialog(Context context, String message, OnClickListener confirmOnClickListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_LIGHT);
        builder.setTitle("提示");
        builder.setIcon(R.drawable.logo11);
        builder.setMessage(message);
        builder.setPositiveButton("确定", confirmOnClickListener);
        builder.setNegativeButton("取消", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.show();
    }

    /**
     *
     * @param context   上下文
     * @param message   内容
     * @param confirmOnClickListener    积极 的点击事件
     * @param positive  积极的按钮文字
     * @param negative  消极的按钮文字
     */
    public static void getDialog(Context context, String message, OnClickListener confirmOnClickListener,String positive,String negative) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_LIGHT);
        builder.setTitle("提示");
        builder.setIcon(R.drawable.logo11);
        builder.setMessage(message);
        builder.setPositiveButton(positive, confirmOnClickListener);
        builder.setNegativeButton(negative, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.show();
    }

    /**
     * 无样式Dialog，无取消按钮
     * @param context
     * @param message                内容
     * @param confirmOnClickListener 确定执行
     */
    public static void getDialogNoStyle(Context context, String message, OnClickListener confirmOnClickListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        TextView tv = new TextView(context);
        tv.setText(message);
        tv.setTextSize(20);
        tv.setPadding(30, 20, 10, 10);
        tv.setTextColor(Color.rgb(30, 30, 30));
        builder.setCustomTitle(tv)
        .setCancelable(false)
        .setIcon(R.drawable.logo11)
        .setPositiveButton("确定",confirmOnClickListener);
        AlertDialog alert = builder.create();
        alert.show();
    }


    /**
     * 无样式Dialog，无取消按钮，确定无执行函数
     * @param context
     * @param message                内容
     */
    public static void getDialogNoStyle(Context context, String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        TextView tv = new TextView(context);
        tv.setText(message);
        tv.setTextSize(20);
        tv.setPadding(30, 20, 10, 10);
        tv.setTextColor(Color.rgb(30, 30, 30));
        builder.setCustomTitle(tv)
                .setCancelable(false)
                .setIcon(R.drawable.logo11)
                .setPositiveButton("确定", new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }



    public static void showDialog(Context context, String message, OnClickListener onClickListener) {

        //对话提示 获取添加车系
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        //自定义对话框
        builder.setTitle("提示");
        builder.setIcon(R.drawable.logo11);
        //设置 自定义布局
        builder.setMessage(message + "");


        builder.setPositiveButton("确定", onClickListener);

        builder.setNegativeButton("取消", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.show();

    }

    public void setSize(Class clazz, int size, AlertDialog dialog) {

//        try {
//            Field field = clazz.getDeclaredField("mAlert");
//            field.setAccessible(true);
//            AlertController controller = (AlertController) field.get(dialog);
//            Log.i("temp", "mAlert:  " + controller.toString());
//            Field viewField = AlertController.class.getDeclaredField("mMessageView");
//            viewField.setAccessible(true);
//            TextView message = (TextView) viewField.get(controller);
//            Log.i("temp", "mAlert:  " + message);
//            message.setText("hello reflect!");
//            message.setTextSize(size);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }


//        AlertDialog dialog = (AlertDialog) getDialog();
        try {
            Field mAlert = clazz.getDeclaredField("mAlert");
            mAlert.setAccessible(true);
            Object alertController = mAlert.get(dialog);

            Field mTitleView = alertController.getClass().getDeclaredField("mTitleView");
            mTitleView.setAccessible(true);

            TextView title = (TextView) mTitleView.get(alertController);
            title.setTextColor(0xff33b5e5);

        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    public static AlertDialog setDialogFontSize(AlertDialog dialog, int size) {
        Window window = dialog.getWindow();
        View view = window.getDecorView();
        setViewFontSize(view, size);

        if (view instanceof ViewGroup) {
            ViewGroup parent = (ViewGroup) view;
            int count = parent.getChildCount();
            for (int i = 0; i < count; i++) {
                setViewFontSize(parent.getChildAt(i), size);
            }
        } else if (view instanceof TextView) {
            TextView textview = (TextView) view;
            textview.setTextSize(size);
        }
        return dialog;
    }

    public static void setViewFontSize(View view, int size) {
        if (view instanceof ViewGroup) {
            ViewGroup parent = (ViewGroup) view;
            int count = parent.getChildCount();
            for (int i = 0; i < count; i++) {
                setViewFontSize(parent.getChildAt(i), size);
            }
        } else if (view instanceof TextView) {
            TextView textview = (TextView) view;
            textview.setTextSize(size);
        }
    }
}
